package com.htthorizon.model.controller;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.htthorizon.common.core.web.controller.BaseController;
import com.htthorizon.common.core.web.domain.AjaxResult;
import com.htthorizon.common.core.web.page.TableDataInfo;
import com.htthorizon.common.log.annotation.Log;
import com.htthorizon.common.log.enums.BusinessType;
import com.htthorizon.common.security.annotation.RequiresPermissions;
import com.htthorizon.model.domain.ModelNeed;
import com.htthorizon.model.request.ModelNeedAddReq;
import com.htthorizon.model.request.ModelNeedSearchReq;
import com.htthorizon.model.service.IModelNeedService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.io.UnsupportedEncodingException;
import java.util.List;
import java.util.Map;
import java.util.Optional;

/**
 * @author Brilliant
 */
@RestController
@RequestMapping("/need")
public class ModelNeedController extends BaseController {

    @Autowired
    private IModelNeedService modelNeedService;

    @GetMapping("/list")
    @ApiOperation("列表")
    @Log(title = "模型管理/模型需求", businessType = BusinessType.QUERY)
    public TableDataInfo list(@RequestParam(value = "status",required = false)Integer status,@RequestParam(value = "needTitle",required = false)String needTitle,@RequestParam(value = "createBy",required = false)String createBy,@RequestParam(value = "modelAuthor",required = false)String modelAuthor,@RequestParam("pageNum")Integer pageNum1,@RequestParam("pageSize")Integer pageSize1){
        ModelNeedSearchReq modelNeed = new ModelNeedSearchReq();
        modelNeed.setNeedTitle(needTitle);
        modelNeed.setModelAuthor(modelAuthor);
        modelNeed.setStatus(status);
        modelNeed.setCreateBy(createBy);
        TableDataInfo tableDataInfo = new TableDataInfo();
        ModelNeedSearchReq modelNeedSearchReq = Optional.ofNullable(modelNeed).orElse(new ModelNeedSearchReq());
        TableDataInfo list= modelNeedService.selectModelNeedList(modelNeedSearchReq,tableDataInfo,pageNum1,pageSize1);
        return list;
    }

    @PostMapping("/add")
    @ApiOperation("新增")
    @RequiresPermissions("model:demand:add")
    @Log(title = "模型管理/模型需求", businessType = BusinessType.INSERT)
    public AjaxResult add(@RequestBody @Valid ModelNeedAddReq modelNeedAddReq) throws UnsupportedEncodingException {
       boolean result= modelNeedService.addModelNeed(modelNeedAddReq);
       return AjaxResult.success(result);
    }

    @GetMapping("/check/{id}")
    @ApiOperation("检查")
    @RequiresPermissions("model:demand:edit")
    @Log(title = "模型管理/模型需求核查", businessType = BusinessType.INSERT)
    public AjaxResult check(@PathVariable("id") Long id, @RequestParam("checkStatus") Integer checkStatus){
        boolean result= modelNeedService.checkStatus(id,checkStatus);
        return AjaxResult.success();
    }

    @PostMapping("/edit")
    @ApiOperation("编辑")
    @RequiresPermissions("model:demand:edit")
    @Log(title = "模型管理/模型需求", businessType = BusinessType.UPDATE)
    public AjaxResult edit(@RequestBody @Valid ModelNeedAddReq modelNeedAddReq){
        DateTime now = DateTime.now();
        modelNeedAddReq.setCheckTime(now);
        boolean result=   modelNeedService.edit(modelNeedAddReq);
        return AjaxResult.success(result);
    }

    @GetMapping("/del/{id}")
    @ApiOperation("删除")
    @RequiresPermissions("model:demand:delete")
    @Log(title = "模型管理/模型需求", businessType = BusinessType.DELETE)
    public AjaxResult edit(@PathVariable("id") Long id){
        boolean result=   modelNeedService.delById(id);
        return AjaxResult.success(result);
    }
}
